<?php

declare (strict_types = 1);

namespace app\api\controller;
use think\App;
use think\facade\Log;
use app\api\service\File as fileUpload;
use think\response\Json;

class File extends Controller
{
    private $fileService;
    public function __construct(App $app,fileUpload $fileService)
    {
        parent::__construct($app);
        $this->fileService = $fileService;
    }

    public function insert():Json
    {
        $user_id = \app\api\service\User::getCurrentLoginUserId();
        $group_id = $this->request->param('group_id',100);
        $file = $_FILES['file'];
        if(empty($file)){
            return $this->renderError('参数错误');
        }

        $filesize = $file['size'];
        if($filesize > 10 * 1024 * 1024){
            return $this->renderError('文件超出10M大小限制');
        }

        $res = (new \app\common\service\FileUpload())->getInsertData($file);
        if(!$res){
            return $this->renderError('文件上传失败');
        }
        //添加上传记录
        $image_id = (new \app\common\service\FileUpload())->addUploadRecords($res,$group_id);
        $res['image_id'] = intval($image_id);
        return $this->renderSuccess($res);
    }

}
